package ar.edu.unsam.algo3.plantasvszombies.ui.windows

import ar.edu.unsam.algo3.plantasvszombies.dominio.Fila
import ar.edu.unsam.algo3.plantasvszombies.ui.ApplicationModels.JardinAppModel
import java.awt.Color
import org.uqbar.arena.bindings.NotNullObservable
import org.uqbar.arena.layout.ColumnLayout
import org.uqbar.arena.layout.HorizontalLayout
import org.uqbar.arena.widgets.Button
import org.uqbar.arena.widgets.Label
import org.uqbar.arena.widgets.Panel
import org.uqbar.arena.widgets.TextBox
import org.uqbar.arena.widgets.tables.Column
import org.uqbar.arena.widgets.tables.Table
import org.uqbar.arena.windows.Dialog
import org.uqbar.arena.windows.SimpleWindow
import org.uqbar.arena.windows.WindowOwner
import org.uqbar.commons.utils.Observable

@Observable
class JardinWindow extends SimpleWindow<JardinAppModel> {

	new(WindowOwner parent) {
		super(parent, new JardinAppModel)
		modelObject.actualizar
	}

	override def createMainTemplate(Panel mainPanel) {
		title = "Plantas Vs Zombies"
		taskDescription = "Bienvenido al juego"
		super.createMainTemplate(mainPanel)
	}

	override protected createFormPanel(Panel mainPanel) {
		val tituloPanel = new Panel(mainPanel)
		tituloPanel.setLayout(new ColumnLayout(1))

		val lblTitulo = new Label(tituloPanel).setText("Jardin")
		lblTitulo.foreground = Color.BLUE
		lblTitulo.fontSize = 20
		lblTitulo.width = 450

		val juegoPanel = new Panel(mainPanel)
		juegoPanel.layout = new ColumnLayout(4)
		val jugadorPanel = new Panel(juegoPanel)
		jugadorPanel.setLayout(new HorizontalLayout)
		new Label(jugadorPanel).setText("Jugador: ").setForeground(Color::BLUE)
		new TextBox(jugadorPanel).setWidth(100).bindValueToProperty("jardin.jugador")

		val recursosPanel = new Panel(juegoPanel)
		recursosPanel.setLayout(new HorizontalLayout)
		new Label(recursosPanel).setText("Recursos: ").setForeground(Color::BLUE)
		new Label(recursosPanel).bindValueToProperty("jardin.recursos")

		val lugaresPanel = new Panel(juegoPanel)
		lugaresPanel.setLayout(new HorizontalLayout)
		new Label(lugaresPanel).setText("Filas Disponibles: ").setForeground(Color::BLUE)
		new Label(lugaresPanel).bindValueToProperty(("filasLibres"))
		new Button(juegoPanel).setCaption("Reiniciar").onClick[|modelObject.iniciar]

		val abmPanel = new Panel(mainPanel)
		abmPanel.setLayout(new HorizontalLayout())
		var btnAgregar = new Button(abmPanel).setCaption("Agregar fila").onClick[|nvaFila]
		btnAgregar.bindEnabledToProperty("activarNvaFila")
		var btnEditar = new Button(abmPanel).setCaption("Editar fila").onClick[|editFila]
		var btnQuitar = new Button(abmPanel).setCaption("Eliminar fila").onClick[|removeFila]
		var btnAtacar = new Button(abmPanel).setCaption("Atacar fila").onClick[|atacarFila]

		val tblPanel = new Panel(mainPanel)
		tblPanel.setLayout(new ColumnLayout(1))
		this.createResultsGrid(tblPanel)

		// Deshabilitar los botones si no hay ningún elemento seleccionado en la grilla.
		var elementSelected = new NotNullObservable("filaSeleccionada")
		btnQuitar.bindEnabled(elementSelected)
		btnEditar.bindEnabled(elementSelected)
		btnAtacar.bindEnabled(elementSelected)

	}

	def nvaFila() {
		val miFila = new Fila
		modelObject.jardin.agregarFila(miFila)
		val dialogo = new FilaWindow(this, miFila)
		dialogo.onCancel[|modelObject.jardin.eliminarFila(miFila)]
		openDialog(dialogo)
	}

	def void editFila() {
		modelObject.resguardar
		val dialogo = new FilaWindow(this, modelObject.filaSeleccionada)
		dialogo.onCancel[|modelObject.restaurar]
		openDialog(dialogo)
	}

	def void removeFila() {
		val pregunta = new MensajeWindow(this, "¿Desea eliminar la fila seleccionada?")
		pregunta.onAccept()[|modelObject.jardin.eliminarFila(modelObject.filaSeleccionada)]
		pregunta.open
		modelObject.actualizar
	}

	def void atacarFila() {
		(new BatallaWindow(this, modelObject.filaSeleccionada)).open
	}

	def void almanaque() {
		new AlmanaqueWindow(this).open
	}

	def void irAlZen() {
		new JardinZenWindow(this, modelObject.jardin.tiposPosibles.get(0), modelObject.jardin).open
	}

	override protected addActions(Panel actionsPanel) {

		new Button(actionsPanel) //
		.setCaption("Almanaque").onClick[|almanaque]

		new Button(actionsPanel).setCaption("JardinZen").onClick[|irAlZen]
		new Button(actionsPanel).setCaption("Salir").onClick[|this.close]
	}

	def protected createResultsGrid(Panel mainPanel) {
		var table = new Table<Fila>(mainPanel, typeof(Fila))
		table.heigth = 158
		table.width = 450
		table.bindItemsToProperty("misFilas")
		table.bindValueToProperty("filaSeleccionada")

		this.describeResultsGrid(table)
	}

	def void describeResultsGrid(Table<Fila> table) {

		new Column<Fila>(table) //
		.setTitle("Tipo").setFixedSize(50).bindContentsToProperty("jardinZen")

		new Column<Fila>(table) //
		.setTitle("Defensa").setFixedSize(55).bindContentsToProperty("defensaTotal")

		new Column<Fila>(table) //
		.setTitle("Ataque").setFixedSize(50).bindContentsToProperty("ataqueTotal")

		new Column<Fila>(table) //
		.setTitle("Salud").setFixedSize(50).bindContentsToProperty("saludTotal")

		new Column<Fila>(table) //
		.setTitle("Plantas").setFixedSize(260).bindContentsToProperty("plantas")

	}

	def openDialog(Dialog<?> dialog) {
		dialog.onAccept[|modelObject.actualizar]
		dialog.open
	}

}
